<?php

namespace App\Http\Controllers\Admin;

use App\Models\Card;
use App\Models\Allowance;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CardController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $status = Card::$cardStatus;
        return view('admin.card.index', compact('status'));
    }

    public function data(Request $request)
    {
        $model = Card::query();
        if ($request->get('card_status')) {
            $model = $model->where('card_status', $request->get('card_status'));
        }
        if ($request->get('card_number')) {
            $model = $model->where('card_number', 'like', '%' . $request->get('card_number') . '%');
        }
        $res = $model->orderBy('id', 'desc')
            ->paginate($request->get('limit', 30))
            ->toArray();

        return $this->table($res['data'], $res['total']);
    }

    /**
     * Store a newly created resource in storage.
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'title'       => 'required|string',
            'sort'        => 'required|numeric',
            'thumb'       => 'required|string',
            'position_id' => 'required|numeric'
        ]);
        if (Advert::create($request->all())) {
            return redirect(route('admin.advert'))->with(['status' => '添加完成']);
        }
        return redirect(route('admin.advert'))->with(['status' => '系统错误']);
    }

    /**
     * 退还油卡
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function enabled(Request $request)
    {
        try {
            $ids = $request->get('ids');
            if (empty($ids)) {
                return $this->error('请选择已收回的油卡');
            }
            // 更新油卡信息
            Card::whereIn('id', $ids)
                ->where('card_status', Card::CARD_STATUS_DISABLED)
                ->update(['card_status' => Card::CARD_STATUS_ENABLED]);

            // 更新油卡补贴状态
            $cardNumber = Card::whereIn('id', $ids)->pluck('card_number')->toArray();
            Allowance::whereIn('card_number', $cardNumber)
                ->where('status', Allowance::STATUS_USEING)
                ->update([
                    'status' => Allowance::STATUS_RETURN,
                ]);

            return $this->success('油卡操作成功');
        } catch (\Exception $e) {
            return $this->error('油卡操作失败');
        }
    }

    /**
     * 油卡丢失
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function loss(Request $request)
    {
        try {
            $ids = $request->get('ids');
            if (empty($ids)) {
                return $this->error('请选择已丢失的油卡');
            }
            // 更新油卡信息
            Card::whereIn('id', $ids)
                ->where('card_status', Card::CARD_STATUS_DISABLED)
                ->update(['card_status' => Card::CARD_STATUS_LOSE]);

            // 更新油卡补贴状态
            $cardNumber = Card::whereIn('id', $ids)->pluck('card_number')->toArray();
            Allowance::whereIn('card_number', $cardNumber)
                ->where('status', Allowance::STATUS_USEING)
                ->update([
                    'status' => Allowance::STATUS_LOSE,
                ]);

            return $this->success('油卡操作成功');

        } catch (\Exception $e) {
            return $this->error('油卡操作失败');
        }
    }
}
